package com.hyl.controller;

import com.hyl.entity.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2023/5/10 10:56
 **/
@Controller
@RequestMapping("/emp") //一定在页面中添加对应的文件夹
public class EmpController {

	@GetMapping("/list") //  http://localhost:8080/SpringMVC_ch09_war_exploded/emp/list?page=1
	public ModelAndView list(
			@RequestParam(name = "page",required = false,defaultValue = "1") Integer currentPage, //当前页
	                         Integer showSize){ //每页显示的数量
		ModelAndView tmp = new ModelAndView();
		tmp.setViewName("emp/list"); //  http://localhost:8080/SpringMVC_ch09_war_exploded/jsp/emp/a.jsp
		return tmp;
	}

	@GetMapping("/add1") //  http://localhost:8080/SpringMVC_ch09_war_exploded/emp/list?page=1
	public ModelAndView add1(HttpServletRequest request){ //每页显示的数量
		 String name = request.getParameter("name");
		 String empNo= request.getParameter("empNo");
		 String departName= request.getParameter("departName");

		 Employee emp = new Employee();
		 emp.setName(name);
		 emp.setDepartName(departName);
		 emp.setEmpNo(empNo);


		ModelAndView tmp = new ModelAndView();
		tmp.setViewName("add"); //  http://localhost:8080/SpringMVC_ch09_war_exploded/jsp/emp/a.jsp
		return tmp;
	}



	@GetMapping("/add")
	public String add(@Valid Employee employee, //数据校验
	                  BindingResult bindingResult){//校验结果
		//验证失败
		if (bindingResult.hasErrors()){
			//验证失败
			return "emp/toadd";
		}
		return "redirect:emp/list";//重定向
	}

	@GetMapping("/toadd")
	public ModelAndView toadd(){ //每页显示的数量
		ModelAndView tmp = new ModelAndView();
		tmp.setViewName("emp/add"); //  http://localhost:8080/SpringMVC_ch09_war_exploded/jsp/emp/a.jsp
		return tmp;
	}

}
